package com.ruoyi.wechat.controller;

import com.ruoyi.wechat.domain.AppUser;
import com.ruoyi.wechat.domain.form.LoginForm;
import com.ruoyi.wechat.domain.vo.AjaxResult;
import com.ruoyi.wechat.domain.vo.LoginData;
import com.ruoyi.wechat.service.IAppUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户接口
 * </p>
 *
 * @author YinHeng
 * @since 2024/3/2
 */
@RestController
@RequestMapping("/user")
public class AppUserController {
    @Autowired
    private IAppUserService appUserService;

    @PutMapping("/bind-phone")
    public AjaxResult bindPhone(@RequestBody LoginForm form) {
        AppUser user = appUserService.bindPhone(form.getEncryptedData(), form.getIv());
        LoginData.UserInfo userInfo = new LoginData.UserInfo();
        BeanUtils.copyProperties(user, userInfo);
        return AjaxResult.success(userInfo);
    }

    /**
     * 更新定位信息
     *
     * @param user
     * @return
     */
    @PutMapping("/set-location")
    public AjaxResult setLocation(@RequestBody AppUser user) {
        appUserService.updateLocation(user.getLat(), user.getLng(), user.getArea());
        return AjaxResult.success();
    }

    /**
     * 编辑个人资料
     *
     * @param user
     * @return
     */
    @PutMapping
    public AjaxResult update(@RequestBody AppUser user) {
        appUserService.update(user);
        return AjaxResult.success();
    }

}
